import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//        +----------+-------------+------+-----+-------------------+-----------------------------+
//        | Field    | Type        | Null | Key | Default           | Extra                       |
//        +----------+-------------+------+-----+-------------------+-----------------------------+
//        | blogId   | int(11)     | NO   | PRI | NULL              | auto_increment              |
//        | title    | varchar(50) | YES  |     | NULL              |                             |
//        | content  | text        | YES  |     | NULL              |                             |
//        | postTime | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
//        | userId   | int(11)     | YES  |     | NULL              |                             |
//        +----------+-------------+------+-----+-------------------+-----------------------------+
//博客的数据库操作封装
public class BlogDao {

    //1.新增博客
    public void insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            //1.连接数据库
            connection = DBUtil.getConnection();

            //2.构造sql语句
            String sql = "insert into blog values(null,?,?,now(),?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3,blog.getUserId());

            //3.执行sql语句
            int ret = statement.executeUpdate();

            //4.判断是否执行成功
            if(ret != 1) {
                System.out.println("新增博客失败!");
            } else {
                System.out.println("新增博客成功!");
            }

            //关闭资源不能在这里,防止前面出错误导致资源泄漏

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //5.关闭资源
            DBUtil.close(connection,statement,null);
        }
    }

    //按照blogId查询对应的博客
    public Blog selectOne(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //1.连接数据库
            connection = DBUtil.getConnection();

            //2.构造sql语句
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);

            //3.执行sql
            resultSet = statement.executeQuery();

            //4.从resultSet中获取Blog的属性
            if(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            DBUtil.close(connection,statement,resultSet);
        }
        return null;

    }

    //讲blog数据库中所有的博客都查询出来
    public List<Blog> selectAll() {

        List<Blog> blogs = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 和数据库建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 SQL
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            // 3. 执行 SQL
            resultSet = statement.executeQuery();
            // 4. 遍历结果集合
            while (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                // 避免博客列表页显示的内容太长. 希望用户点到博客详情页里再看到这个详细正文的.
                if(content != null && content.length() >= 50) {
                    content = content.substring(0,50) + ".....";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, resultSet);
        }
        return blogs;
    }

    //删除指定的博客
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            //1.连接数据库
            connection = DBUtil.getConnection();

            //2.构造sql语句
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);


            //3.执行sql语句
            int ret = statement.executeUpdate();

            //4.判断是否执行成功
            if(ret != 1) {
                System.out.println("删除博客失败!");
            } else {
                System.out.println("删除博客成功!");
            }

            //关闭资源不能在这里,防止前面出错误导致资源泄漏

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //5.释放资源
            DBUtil.close(connection,statement,null);
        }
    }

}
